package com.ecy.quadratic.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ecy.quadratic.R;
import com.ecy.quadratic.beans.support.RecommendBookEvent;
import com.ecy.quadratic.component.DaggerNovelMainComponent;
import com.ecy.quadratic.manager.SettingManager;
import com.ecy.quadratic.reader.base.BaseFragment;
import com.ecy.quadratic.reader.bean.BookMixAToc;
import com.ecy.quadratic.reader.bean.Recommend;
import com.ecy.quadratic.reader.component.AppComponent;
import com.ecy.quadratic.reader.ui.activity.ReadActivity;
import com.ecy.quadratic.reader.ui.adapter.TocListAdapter;
import com.ecy.quadratic.reader.utils.ToastUtils;
import com.ecy.quadratic.ui.contract.BookSectionsContract;
import com.ecy.quadratic.ui.presenter.BookSectionsPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;


/**
 * Created by Administrator on 2016/3/31.
 */
public class BookSectionsFragment extends BaseFragment implements BookSectionsContract.View {

    private static final String BOOK_ID = "book_id";
    @Bind(R.id.comicdetail_sections_gridview)
    ListView mGridView;

    @Inject
    BookSectionsPresenter mSectionsPresenter;

    private Recommend.RecommendBooks recommendBooks;
    private TocListAdapter mTocListAdapter;
    private String bookId = "";
    private List<BookMixAToc.mixToc.Chapters> mChapterList = new ArrayList<>();
    private int currentChapter = 1;

    public static BookSectionsFragment newInstance(String bookId) {
        BookSectionsFragment fragment = new BookSectionsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(BOOK_ID, bookId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_book_sections;
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerNovelMainComponent.builder()
                .appComponent(appComponent)
                .build()
                .inject(this);
    }

    @Override
    public void attachView() {
        if (mSectionsPresenter != null) {
            mSectionsPresenter.attachView(this);
        }
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mSectionsPresenter != null) {
            mSectionsPresenter.detachView();
        }
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initData() {
        bookId = getArguments().getString(BOOK_ID);
    }

    @Override
    public void configViews() {
        initTocList();
    }

    private void initTocList() {
        mTocListAdapter = new TocListAdapter(getContext(), mChapterList, bookId, 1);
        mGridView.setAdapter(mTocListAdapter);
        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ReadActivity.startActivity(getActivity(), recommendBooks, position + 1);
//                Recommend.RecommendBooks recommendBooks = new Recommend.RecommendBooks();
//                recommendBooks.title = data.title;
//                recommendBooks._id = data._id;
//                recommendBooks.cover = data.cover;
//                recommendBooks.lastChapter = data.lastChapter;
//                recommendBooks.updated = data.updated;
//                mTocListPopupWindow.dismiss();
//                currentChapter = position + 1;
//                mTocListAdapter.setCurrentChapter(currentChapter);
//                startRead = false;
//                showDialog();
//                readCurrentChapter();
//                hideReadBar();
            }
        });
        mSectionsPresenter.getBookMixAToc(bookId, "chapters");
    }

    @Override
    public void netError(int chapter) {
        hideDialog();//防止因为网络问题而出现dialog不消失
        if (Math.abs(chapter - currentChapter) <= 1) {
            ToastUtils.showToast(R.string.net_error);
        }
    }

    /**
     * 利用线程池定时执行动画轮播
     */
    @Override
    public void onStart() {
        super.onStart();
        int pos[] = SettingManager.getInstance().getReadProgress(bookId);
        mTocListAdapter.setCurrentChapter(pos[0]);
        mGridView.setSelection(pos[0] - 1);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    /**
     * 加载章节列表
     *
     * @param list
     */
    @Override
    public void showBookToc(List<BookMixAToc.mixToc.Chapters> list) {
        mChapterList.clear();
        mChapterList.addAll(list);
        mTocListAdapter.notifyDataSetChanged();
        int pos[] = SettingManager.getInstance().getReadProgress(bookId);
        mTocListAdapter.setCurrentChapter(pos[0]);
        mGridView.setSelection(pos[0] - 1);
    }


    @Override
    public void showError() {

    }

    @Override
    public void complete() {

    }

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void updateRecommendBook(RecommendBookEvent event) {
        recommendBooks = event.book;
    }
}
